import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import { getStorage } from '@/utils/storage.js'
import { defaultRoutes } from './routerList.js'
const Router = createRouter({
    routes: defaultRoutes,
    history: createWebHashHistory()
})


Router.beforeEach((to, from, next) => {

    const token = getStorage('TOKEN')
    if (token) {
        if (to.name === 'Login') {
            next('/')
        } else {
            next()
        }
    } else {

        if (to.name === 'Login') {
            next()
        } else {
            next({
                name: 'Login',
                query: {
                    redirect: to.name,
                    ...to.query
                }
            })
        }
    }
})
export default Router
